//配置axios的全局基本路径
axios.defaults.baseURL = 'http://localhost:8080'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios


//拦截器-------------------------------------------------------
//axios前置拦截器，每次请求都会携带token
axios.interceptors.request.use(config => {
    //携带token
    let token = localStorage.getItem("token");//将我们存的token取出:9806a99e-5298-4916-8409-9df211a1e68c
    if (token) {
        //将我们存的token赋值给请求头中的‘token’，这样每次请求就都携带token
        config.headers['token'] = token;//9806a99e-5298-4916-8409-9df211a1e68c
    }
    //返回的头信息，不要注释！！！！
    return config;
}, error => {
    Promise.reject(error);
});

//axios后置拦截器
axios.interceptors.response.use(result => {
    console.log(result.data + "jjjjjjj");
    let data = result.data;
    if (!data.success && data.message === "noLogin"){
        //删除数据
        console.log("xxooxooo")
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        //跳转登录页面
        location.href = "login.html";
    }
    console.log(result);
    return result;
}, error => {
    Promise.reject(error);
})



//自己写静态资源拦截器
let url = location.href;
//不是登录 也不是注册
if(url.indexOf("login.html") ===-1
    && url.indexOf("register.html") ===-1
    && url.indexOf("callback.html") ===-1
    && url.indexOf("binder.html") ===-1
){
    //我就应该阻止
    let logininfo = localStorage.getItem("logininfo");
    if(!logininfo){
        //说明没有登录
        location.href = "login.html";
    }
}


/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 url：页面传来的地址
 */
function parseUrlParams2Obj(url) {// http://bugtracker.itsource.cn/callback.html?code=061IduGa1839qC02jlIa1NmHxX1IduGX&state=1
    //获取请求参数                      以？分割，取后一位//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStr = url.substring(url.indexOf("?") + 1);
    //                            再以&分割//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramStrs = paramStr.split("&");
    let paramObj = {};
    for (let i = 0; i < paramStrs.length; i++) {
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;// 动态给对象paramObj设置属性paramName和值paramValue
    }
    return paramObj;
}
